class Solution(object):
    def sortArrayByParityII(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        even = 0
        odd = 1
        n = len(nums)
        while even < n and odd < n:
            while even < n and nums[even] % 2 == 0:
                even += 2
            while odd < n and nums[odd] % 2 == 1:
                odd += 2
            if even >= n or odd >= n:
                return nums
            else:
                nums[odd], nums[even] = nums[even], nums[odd]
                odd += 2
                even += 2
        return nums




data = Solution()
nums = [4, 2, 5, 7]
print(data.sortArrayByParityII(nums))
